﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace LdCms.Web.Controllers
{
    using LdCms.Web.Models;
    using LdCms.Web.Services;
    using LdCms.IBLL.Sys;
    using LdCms.Common.Json;
    using LdCms.Model.Member;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbEntitiesContext;


    /// <summary>
    /// 首页
    /// </summary>
    public class HomeController : BaseController
    {
        private readonly SiteConfig SiteConfig;
        private readonly IBaseManager BaseManager;
        private readonly IConfigService ConfigService;
        public HomeController(IOptions<SiteConfig> SiteConfig, IBaseManager BaseManager, IConfigService ConfigService) : base(BaseManager)
        {
            this.SiteConfig = SiteConfig.Value;
            this.BaseManager = BaseManager;
            this.ConfigService = ConfigService;
        }
        public override IActionResult Index()
        {
            try
            {
                int systemId = BaseSystemConfig.SystemID;
                string companyId = SiteConfig.CompanyID;
                var entity = ConfigService.GetConfig(systemId, companyId);
                string homeUrl = string.IsNullOrEmpty(entity.HomeUrl) ? SiteConfig.HomeUrl : entity.HomeUrl;
                return Redirect(homeUrl);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public IActionResult Error()
        {
            try
            {
                return View();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
